﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Common.Standard
{
    public class StringHelper
    {
        /// <summary>
        /// 生成随机码
        /// </summary>
        /// <param name="type"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string RandCode(int length = 6, RandCodeType type = RandCodeType.Number)
        {
            if (length < 1)
            {
                throw new Exception("长度错误");

            }
            StringBuilder sb = new StringBuilder(length);
            switch (type)
            {
                case RandCodeType.Word:
                    {
                        for (int i = 0; i < length; i++)
                        {

                            Random rand = new Random((int)DateTime.Now.Ticks + i);
                            Random rand_num = new Random((int)DateTime.Now.Ticks + i);
                            var number = rand_num.Next(10000) % 26;
                            var seed = rand.Next(1000);
                            var ch = 'a';
                            if (seed % 2 == 0)
                            {
                                ch = (char)('a' + number);

                            }
                            else
                            {
                                ch = (char)('A' + number);
                            }
                            sb.Append(ch.ToString());

                        }
                    }
                    break;
                case RandCodeType.Mix:
                    {
                        for (int i = 0; i < length; i++)
                        {

                            Random rand = new Random((int)DateTime.Now.Ticks + i);
                            Random rand_num = new Random((int)DateTime.Now.Ticks + i);
                            var current_randnubmer = rand_num.Next(10000);
                            var number = current_randnubmer % 26;
                            var seed = rand.Next(1000);
                            var ch = 'a';
                            var flag = seed % 3;
                            if (flag == 0)
                            {
                                ch = (char)('a' + number);

                            }
                            else if (flag == 1)
                            {
                                ch = (char)('A' + number);
                            }
                            else
                            {
                                ch = (char)('0' + current_randnubmer % 10);
                            }
                            sb.Append(ch.ToString());

                        }
                    }
                    break;
                default:
                    {

                        for (int i = 0; i < length; i++)
                        {
                            Random rand = new Random((int)DateTime.Now.Ticks + i);
                            var num = rand.Next(10000) % 10;
                            sb.Append(num);
                        }
                    }

                    break;
            }
            return sb.ToString();
        }
        public static int timeToStamp(DateTime time)
        {
            return (int)(TimeZoneInfo.ConvertTimeToUtc(time) - TimeZoneInfo.ConvertTimeToUtc(new System.DateTime(1970, 1, 1))).TotalSeconds-8*3600;
        }
        public static string timeToStamp11(DateTime time)
        {
            return ((TimeZoneInfo.ConvertTimeToUtc(time) - TimeZoneInfo.ConvertTimeToUtc(new System.DateTime(1970, 1, 1))).TotalMilliseconds - 8 * 3600*1000).ToString();
        }
        /// <summary>
        /// 10位转时间
        /// </summary>
        /// <param name="stamp"></param>
        /// <returns></returns>
        public static DateTime stampToTime(int stamp)
        {
            DateTime dtStart = TimeZoneInfo.ConvertTimeToUtc(new DateTime(1970, 1, 1));
            return new DateTime(1970, 1, 1).AddSeconds(stamp+ 8 * 3600);
        }
    }

    public enum RandCodeType
    {
        /// <summary>
        /// 数字
        /// </summary>
        Number = 1,
        /// <summary>
        /// 字母
        /// </summary>
        Word = 2,
        /// <summary>
        /// 数字，字母混合
        /// </summary>
        Mix = 3
    }
}
